package com.syf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.syf.model.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syf.mapper.BsUserMapper;
import com.syf.domain.store.BsUser;
import com.syf.service.BsUserService;

import java.util.List;

/**
*
*
*@author syf
*@create 2022-02-15 13:54
*/
@Slf4j
@Service
public class BsUserServiceImpl extends ServiceImpl<BsUserMapper, BsUser> implements BsUserService{

    /**
     * 用户 登录/注册
     *
     * @param bsUser
     * @return
     */
    @Override
    public R login(BsUser bsUser) {
        LambdaQueryWrapper<BsUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BsUser::getUsername,bsUser.getUsername());
        List<BsUser> userList = list(wrapper);

        //【1】登录
        if(userList.size() >= 1){

            BsUser user = userList.get(0);
            log.info("用户登录：{}" , user.getUsername());
            if(user.getPassword().equals(bsUser.getPassword())){
                return R.ok(user);  //登录成功
            }else {
                return R.fail("用户名或密码错误");
            }
        }

        //【2】注册
        log.info("用户注册：{}" , bsUser.getUsername());
        boolean save = save(bsUser);
        if(save){
            return R.ok(bsUser);  //注册成功
        }
        return null;
    }
}
